/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2021/5/20
 * Time: 23:00
 */
#include "CustomChart.h"
#include <QtCharts>
#include <QtWidgets>

QT_CHARTS_USE_NAMESPACE

int main(int argc, char * argv[])
{
    QApplication a(argc, argv);

    CustomChart * chart = new CustomChart();
    chart->setAnimationOptions(QChart::AllAnimations);

    QStringList airplane_type;// 总图categories
    airplane_type << "运输机"
                  << "歼击机"
                  << "直升机"
                  << "轰炸机";

    QStringList airport_list;// 子图categories
    airport_list << "机场A"
                 << "机场B"
                 << "机场C"
                 << "机场D"
                 << "机场E"
                 << "机场F"
                 << "机场G"
                 << "机场H";

    CustomSeries * all_series = new CustomSeries(chart);
    all_series->SetCategories(airplane_type);
    all_series->SetMaxValue(100);

    all_series->setName("所有机场机型数量");

    for(int type = 0; type < airplane_type.count(); ++type) {
        auto plane_type_series = new CustomSeries(chart);
        plane_type_series->SetCategories(airport_list);
        plane_type_series->SetMaxValue(50);

        all_series->MapCustomSeries(type, plane_type_series);

        for(int airport = 0; airport < airport_list.count(); ++airport) {
            plane_type_series->MapCustomSeries(airport, all_series);
            plane_type_series->setName(airport_list.at(airport) + "-机型数量分布");
        }

        QObject::connect(plane_type_series, &CustomSeries::clicked,
                         chart, &CustomChart::HandleBarClicked);
    }

    QObject::connect(all_series, &QBarSeries::clicked, chart, &CustomChart::HandleBarClicked);

    // 初始化数据

    QBarSet * type_barset = new QBarSet("项目1");
    for(int type = 0; type < airplane_type.count(); ++type) {
        QBarSet * airport_barset = new QBarSet(airplane_type.at(type));
        for(int airport = 0; airport < airport_list.count(); ++airport) {
            int s = QRandomGenerator::global()->bounded(20);
            *airport_barset << s;
        }
        all_series->OwnSeries(type)->append(airport_barset);
        int sum = airport_barset->sum();
        *type_barset << sum;
    }
    all_series->append(type_barset);

    chart->ChangeSeries(all_series);
    chart->setTitle(all_series->name());

//    chart->axes(Qt::Horizontal).first()->setGridLineVisible(false);
    QChartView * view = new QChartView(chart);
    view->setRenderHint(QPainter::Antialiasing);

    view->show();
    return QApplication::exec();
}